using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AttackChecker : MonoBehaviour
{
    Collider2D left, right, current;
    Collider2D[] temp = new Collider2D[10];
    ContactFilter2D filter;

    // Start is called before the first frame update
    void Start()
    {
        left = transform.Find("Left").GetComponent<Collider2D>();
        right = transform.Find("Right").GetComponent<Collider2D>();

        filter = new ContactFilter2D();
        filter.layerMask = LayerMask.GetMask("Attack");
        filter.useLayerMask = true;
    }

    public IAttack GetAttackTarget(SoldierController.Direction dir)
    {
        switch (dir)
        {
            case SoldierController.Direction.Null:
                current = null;
                break;
            case SoldierController.Direction.Left:
                current = left;
                break;
            case SoldierController.Direction.Right:
                current = right;
                break;
        }

        int len = 0;
        if (current != null)
        {
            len = current.OverlapCollider(filter, temp);
        }

        if (len > 0)
        {
            return temp[0].GetComponent<IAttack>();
        }
        else
        {
            return null;
        }
    }

    public void UseSword(bool isSowrd)
    {
        gameObject.SetActive(isSowrd);
    }
}
